package com.yxx.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yxx.business.entity.*;
import com.yxx.business.service.PhaseService;
import com.yxx.common.utils.DateUtils;
import com.yxx.common.core.domain.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.core.service.impl.ServiceImplPlus;
import com.yxx.business.mapper.TbDemolitionPhaseMapper;
import com.yxx.business.service.ITbDemolitionPhaseService;

/**
 * 拆除处置阶段Service业务层处理
 *
 * @author yxx
 * @date 2025-08-16
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbDemolitionPhaseServiceImpl extends ServiceImplPlus<TbDemolitionPhaseMapper, TbDemolitionPhaseDo> implements ITbDemolitionPhaseService, PhaseService {

    private final TbDemolitionPhaseMapper tbDemolitionPhaseMapper;

    /**
     * 查询拆除处置阶段分页结果
     *
     * @param tbDemolitionPhaseQueryDto 拆除处置阶段查询实体
     * @return 拆除处置阶段分页
     */
    @Override
    public PageResult<TbDemolitionPhaseVo> selectTbDemolitionPhaseVoPage(TbDemolitionPhaseQueryDto tbDemolitionPhaseQueryDto) {
        startPage();
        PageResult<TbDemolitionPhaseVo> page
                = super.getMyBatisPageResult(selectTbDemolitionPhaseDoList(tbDemolitionPhaseQueryDto), TbDemolitionPhaseVo.class);
        clearPage();
        return page;
    }

    /**
     * 查询拆除处置阶段Vo列表
     *
     * @param tbDemolitionPhaseQueryDto 拆除处置阶段查询实体
     * @return 拆除处置阶段集合
     */
    @Override
    public List<TbDemolitionPhaseVo> selectTbDemolitionPhaseVoList(TbDemolitionPhaseQueryDto tbDemolitionPhaseQueryDto) {
        return super.convertVoList(tbDemolitionPhaseMapper.selectTbDemolitionPhaseList(tbDemolitionPhaseQueryDto), TbDemolitionPhaseVo.class);
    }

    /**
     * 查询拆除处置阶段Do列表
     *
     * @param tbDemolitionPhaseQueryDto 拆除处置阶段查询实体
     * @return 拆除处置阶段集合
     */
    @Override
    public List<TbDemolitionPhaseDo> selectTbDemolitionPhaseDoList(TbDemolitionPhaseQueryDto tbDemolitionPhaseQueryDto) {
        return tbDemolitionPhaseMapper.selectTbDemolitionPhaseList(tbDemolitionPhaseQueryDto);
    }

    /**
     * 查询单个拆除处置阶段
     *
     * @param demolitionId 拆除处置阶段主键
     * @return 拆除处置阶段单个
     */
    @Override
    public TbDemolitionPhaseVo selectTbDemolitionPhaseVoByDemolitionId(Long demolitionId) {
        return super.convertBean(tbDemolitionPhaseMapper.selectTbDemolitionPhaseByDemolitionId(demolitionId), TbDemolitionPhaseVo.class);
    }

    /**
     * 新增拆除处置阶段
     *
     * @param tbDemolitionPhaseQueryDto 拆除处置阶段编辑实体
     * @return 结果
     */
    @Override
    public int insertTbDemolitionPhase(TbDemolitionPhaseEditDto tbDemolitionPhaseQueryDto) {
        tbDemolitionPhaseQueryDto.setCreateById(getUserIdOrNotLogged());
        tbDemolitionPhaseQueryDto.setCreateBy(getUserNameOrNotLogged());
        tbDemolitionPhaseQueryDto.setCreateTime(DateUtils.getNowDate());
        return tbDemolitionPhaseMapper.insertTbDemolitionPhase(super.convertT(tbDemolitionPhaseQueryDto));
    }

    /**
     * 修改拆除处置阶段
     *
     * @param demolitionId 拆除处置阶段主键
     * @param tbDemolitionPhaseQueryDto 拆除处置阶段编辑实体
     * @return 结果
     */
    @Override
    public int updateTbDemolitionPhase(Long demolitionId, TbDemolitionPhaseEditDto tbDemolitionPhaseQueryDto) {
        tbDemolitionPhaseQueryDto.setUpdateBy(getUserNameOrNotLogged());
        tbDemolitionPhaseQueryDto.setUpdateTime(DateUtils.getNowDate());
        return tbDemolitionPhaseMapper.updateTbDemolitionPhase(super.convertT(tbDemolitionPhaseQueryDto).setDemolitionId(demolitionId));
    }

    /**
     * 批量删除拆除处置阶段
     *
     * @param demolitionIds 需要删除的拆除处置阶段主键集合
     * @return 结果
     */
    @Override
    public int deleteTbDemolitionPhaseByDemolitionIds(List<Long> demolitionIds) {
        return tbDemolitionPhaseMapper.deleteTbDemolitionPhaseByDemolitionIds(demolitionIds);
    }

    /**
     * 删除单个拆除处置阶段信息
     *
     * @param demolitionId 拆除处置阶段主键
     * @return 结果
     */
    @Override
    public int deleteTbDemolitionPhaseByDemolitionId(Long demolitionId) {
        return tbDemolitionPhaseMapper.deleteTbDemolitionPhaseByDemolitionId(demolitionId);
    }

    @Override
    public TbEquipmentPhaseVo getPhase(TbEquipmentPhaseDto tbEquipmentPhaseDto) {
        return convertBean(tbDemolitionPhaseMapper.selectOne(
                        new LambdaQueryWrapper<TbDemolitionPhaseDo>().eq(TbDemolitionPhaseDo::getEngineeringId, tbEquipmentPhaseDto.getEngineeringId()).eq(TbDemolitionPhaseDo::getProjectId, tbEquipmentPhaseDto.getProjectId())),
                TbEquipmentPhaseVo.class);
    }

    @Override
    public int savePhase(TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        Long l = tbDemolitionPhaseMapper.selectCount(new LambdaQueryWrapper<TbDemolitionPhaseDo>().eq(TbDemolitionPhaseDo::getEngineeringId, tbEquipmentPhaseVo.getEngineeringId()));
        if (l > 0) {
            tbDemolitionPhaseMapper.update(convertT(tbEquipmentPhaseVo), new LambdaUpdateWrapper<TbDemolitionPhaseDo>().eq(TbDemolitionPhaseDo::getEngineeringId, tbEquipmentPhaseVo.getEngineeringId()).eq(TbDemolitionPhaseDo::getProjectId, tbEquipmentPhaseVo.getProjectId()));
        } else {
            tbDemolitionPhaseMapper.insert(convertT(tbEquipmentPhaseVo));
        }
        return 1;
    }

    @Override
    public int updatePhase(Long id, TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        return tbDemolitionPhaseMapper.updateById(convertT(tbEquipmentPhaseVo).setDemolitionId(id));
    }
}
